<?php
namespace BoxEdu\Controller\Home;
//use Think\Controller;
class IndexController extends CommonController {
	public function _initialize() {
		parent::_initialize();
		$this->show_user_info = $this->user_info;

	}

	function test() {
		// dump($this->user_id);
		// $row=array(
		//     'num'=>10,
		//     'user_id'=>100334,
		//     'public'=>0,
		//     'type'=>'xuebazi',

		// );

		// Logic("BoxSns","Log")->add($row,2);
		//$this->display();
		//$r=BoxModel("edu_subject_order")->where("order_sn in ('G9038339774311d','G9072381067128d','G9154883214470d','G9222062591491d','G9223644827967d')")->save(array('offline_subject_ids'=>1));
		//$data=BoxModel("edu_subject_order")->where("order_sn in ('G9038339774311d','G9072381067128d','G9154883214470d','G9222062591491d','G9223644827967d')")->select();
		//dump($r);
		// $data = BoxModel("edu_subject_order")->where(array('subjectid' => 66))->select();
		// $userModel = BoxModel("edu_user");
		// foreach ($data as $key => $value) {
		// 	$user = BoxModel("edu_user")->where(array('user_id' => $value['user_id']))->find();
		// 	$subjectids = json_decode($user['subjectids'], true);
		// 	if ($subjectids[66]) {
		// 		$subjectids[66]['end_date'] = "2017-05-22";
		// 		$user['subjectids'] = json_encode($subjectids);
		// 		$r = $userModel->where(array('user_id' => $user['user_id']))->save($user);
		// 	}

		// }

	}

	public function index() {
		$this->icon = array(
			'1' => 'glyphicon-pencil',
			'2' => 'glyphicon-road',
			'3' => 'glyphicon-book',
			'4' => 'glyphicon-star-empty',
		);

		$this->nav_cur = "Index";
		if ($this->user_info['user_id'] && !$this->is_moblie) {
			// $this->jump("", "Index/my_subject");
		}
		$this->display();
	}

	function subjectList() {
		$this->web_title = "选课中心 - 考试突击";
		$this->display();
	}

	public function my_subject() {
		//dump($this->user_info);
		$this->web_title = "我的班级 - 考试突击";
		$this->nav_cur = "Home";
		if (!$this->user_info['user_id']) {
			$this->jump("", "/Index/login");
		}
		if ($user_id = I('user_id')) {
			$this->show_user_info = BoxModel("system_member")->load($user_id);
		}
		$this->display();
	}

	function hot() {
		$this->display();
	}

	function earn() {
		if (!$this->user_info['user_id']) {
			$this->jump("", "/Index/login");
		}
		$this->display();
	}

	public function class_info() {
		$this->class_id = I('class_id');
		$this->class_data = BoxModel("edu_subject_class")->where(array('id' => $this->class_id))->find();

	}

	public function subject() {

		$this->class_info();
		$this->class_nav = "subject";
		$this->nav_cur = "selectSubject";
		$this->web_title = $this->class_data['name'] . "学院 - 考试突击";
		$subject_list = BoxModel("edu_subject")->get(array('subject_class' => $this->class_id, 'is_show' => 1), "50", "order_by");
		$this->subject_list = $subject_list['list'];
		//  dump( $this->subject_list);
		$this->display();
	}

	public function subject_detail() {

		$this->subjectid = $subjectid = I('subject_id');
		$this->nav_cur = "selectSubject";
		$this->data = BoxModel("EduSubject")->load($subjectid);
		///	dump($this->data);
		/*******线下课程**/
		$school_id = $this->user_info['school_id'];
		$param = array(
			'school_id' => $school_id,
			'subjectid' => $subjectid,
		);
		$this->web_title = $this->data['subject'] . " - 考试突击";
		$this->offline_subject = Logic("BoxEdu", "Subject")->get_offline_subject_price($param);
		$this->buy_count = BoxModel("edu_subject_order")->where(array('subjectid' => $subjectid, 'status' => array('in', "2,4")))->count();
		$this->display();
	}

	public function down() {

		$id = I('id');
		$this->nav_cur = "selectSubject";
		$model = BoxModel("edu_attach");
		$data = $model->where(array('attid' => $id))->find();
		$data['attext'] = stripslashes(htmlspecialchars_decode($data['attext']));
		$data['active_text'] = stripslashes(htmlspecialchars_decode($data['active_text']));
		$this->data = $data;
		$this->web_title = $this->data['title'] . "下载 - 考试突击";
		$this->display();
	}

	//下载获得积分
	public function down_user() {
		ignore_user_abort(true);
		$id = I('id');
		$data = BoxModel("edu_attach")->where(array('attid' => $id))->find();
		$where = array(
			'attid' => $id,
			'user_id' => $this->user_info['user_id'],
		);
		$r = BoxModel("edu_attach_user")->where($where)->find();
		if (!$r) {
			$bool = BoxModel("edu_attach_user")->add($where);
			if ($bool) {
//下载获得积分
				$row = array(
					'num' => $data['xuebazi'],
					'subjectid' => $data['class_id'],
					'title' => $data['atttitle'],
					'url' => HomeUrl . U('Index/down', array('id' => $data['attid'])),
					'event' => $data['type'] == 1 ? "00021" : "00002",
				);
				Logic("BoxEdu", "User")->add_xuebazi($row);
			}
		}
	}

	public function registerForExamination() {
		$this->class_id = $class_id = I('class_id');
		$this->subjectid = $subjectid = I('subjectid');
		$this->class_info = BoxModel("edu_subject_class")->where(array('id' => $class_id))->find();
		$where = array(
			'user_id' => $this->user_id,
			'classid' => $class_id,
		);
		$model = BoxModel('edu_apply_examination');
		$r = $model->where($where)->find();
		$id = $r['id'];
		$apply = array();
		if ($id) {
			$apply = $model->load($id);
		}
		$user_info = $this->user_info;
		unset($user_info['id']);
		unset($user_info['status']);
		unset($user_info['remark']);
		$apply = array_merge($user_info, $apply);
		//		dump($apply);
		$this->apply = $apply;
		$this->display();
	}

	public function doRegisterForExamination() {
		$class_id = I('class_id');
		$subjectid = I('subjectid');
		$class_info = BoxModel("edu_subject_class")->where(array('id' => $class_id))->find();
		$subjec_info = BoxModel("edu_subject")->where(array('subjectid' => $subjectid))->find();
		$model = BoxModel('edu_apply_examination');
		$param = $model->create();
		$data = array_diff(I(), $param);
		$data['class_name'] = $class_info['name'];
		$data['subject_name'] = $subjec_info['subject'];
		//dump($class_info);exit();
		$examination_id = I('id');
		$info = $this->_upload();
		if (is_array($info)) {
			foreach ($info as $k => $one) {
				$data[$k] = array(
					'file_url' => $one['savepath'] . $one['savename'],
					'file_name' => $one['name'],
				);
			}
		}
		$row = array(
			'classid' => $class_id,
			'subjectid' => $subjectid,
			'user_id' => $this->user_id,
			'money' => $subjec_info['examination_fee'],
			'add_time' => date("Y-m-d H:i:s"),
			'status' => 1,
			'data' => serialize($data),
		);
		$model->startTrans();
		if ($examination_id) {
			$r = $model->where(array('id' => $examination_id))->save($row);
		} else {
			$examination_id = $model->add($row);
		}
		if ($examination_id) {
			exit(json_encode(array('error' => 0, 'msg' => $examination_id)));
		} else {
			exit(json_encode(array('error' => 1, 'msg' => "报考信息提交失败")));
		}

		// //生成订单
		//        if($examination_id){
		//           // $HomeUrl = BOX_MODULE_NAME == "Wap" ? MEduUrl : EduUrl;
		//            $suceess_url = HomeUrl . U('User/apply_examination');
		//            $row = array(
		//                'store_id' => 1,
		//                'user_id' => $this->user_id,
		//                'payment_id' => intval(I('pay_type')),
		//                'order_amount' => $class_info['fee'],
		//                'goods_amount' =>$class_info['fee'],
		//                'order_type' => 'examination',
		//                'suceess_url' => $suceess_url,
		//                'examination_id'=>$examination_id,
		//                'type'          =>'examination',
		//                'title' => '报考名称：' . $class_info['name'],
		//                'body' => '报考名称：' . $class_info['name'] ,
		//            );
		//            $orderData = Logic("BoxMall", "Order")->add($row);
		//
		//        }
		//        if($r && $orderData){
		//            $model->commit();
		//            $order_sn=$orderData['order_sn'];
		//            if ($row['order_amount'] > 0) {//在线结算金额如果大于0
		//                echo json_encode($this->return_array(0, HomeUrl . "/BoxPay/Order/index/order_sn/" . $order_sn . ".html"));
		//            } else {
		//                session("pay_verify", true);
		//                A("BoxPay/Order", "", 1)->succeed($order_sn);
		//                if ($r) {
		//                    echo json_encode($this->return_array(0, $suceess_url));
		//                } else {
		//                    echo json_encode($this->return_array(1, "订单提交失败"));
		//                }
		//            }
		//
		//        }else{
		//            $model->rollback();
		//            $this->jump("提交失败");
		//        }
	}

	public function course() {
		$this->class_info();
		$this->class_nav = "course";
		$this->subject_list = BoxModel("edu_subject")->get(array('S.subject_class' => $this->class_id));
		$this->nav_cur = "selectSubject";
		//dump($this->subject_comment);
		$this->display();
	}

	public function rank() {
		$this->class_nav = "rank";
		$this->class_info();
		$this->display();
	}

	public function voice() {
		$this->class_nav = "voice";
		$this->class_info();
		$this->subject_comment = BoxModel("edu_subject_order")->getComment(array('class_id' => $this->class_id), 5);
		$this->show_page($this->subject_comment['page']);
		$this->display();
	}

	/*****校园动态**/
	public function timeline() {
		$this->timeListWhere = "1";
		$this->tab = $type = I('type', "tab1");
		if ($user_id = I('user_id')) {
			$this->show_user_info = Logic("BoxEdu", "User")->info($user_id);
			$this->to_user_id = $user_id;
			$class_ids = $this->show_user_info['classids'];
		}
		if ($this->to_user_id == $this->user_info['user_id']) {
			$this->nav_cur = "User";
		} else {
			/**访友*/
			Logic("BoxEdu", "User")->visit($this->to_user_id);
		}
		/*******学员动态***/
		if ($type == "tab1") {
			$this->parm .= $user_id;
			$this->str_timeline = "timeline";
		}
		/*******班级动态***/
		if ($type == "tab2") {
			$this->parm = $class_ids;
			$this->str_timeline = "class_timeline";
		}
		/*******访友***/
		if ($type == "tab3") {
			$this->parm = $user_id;
			$this->str_timeline = "user_visit";
		}
		$this->display();
	}

	/******班级动态展示***/
	public function class_new_info() {
		$id = I('id');
		$this->data = BoxModel("SnsTopic")->load($id);
		//  dump($this->data);
		$this->display();
	}

	/*****学员心声**/
	public function xueyuan() {
		$this->title = "学员心声";
		if ($this->subjectid = I('subjectid')) {
			$this->where = "subjectid='$this->subjectid'";
			$data = BoxModel("EduSubject")->load($this->subjectid);
			$this->title = $data['subject'];
		}
		$data = BoxModel("SnsComment")->get("subject_order", $this->where, 10, "comment_id desc");
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->display();
	}

	/*******登录***/
	public function login() {
		// if ($this->user_info['user_id']) {
		//            $this->jump("", "/");
		//        }
		$this->display("Index/login");
	}

	/*********发送手机验证码****/
	public function send_code() {
		$code = rand(100000, 999999);
		$tel = I('tel');
		if (!preg_match('/^1\d{10}$/', $tel)) {
			exit(json_encode(array(
				'error' => 1,
				'msg' => '手机号必须是1开头的11位数字',
			)));
		}
		/*******发送短信******/
		$r = sms($tel, "验证码：" . $code . "【盛明体育科技】");
		session("tel_code", $code);
		exit(json_encode(array(
			'error' => 0,
			'msg' => $code,
		)));
		if ($r === true) {
			session("tel_code", $code);
			exit(json_encode(array(
				'error' => 0,
				'msg' => "验证码已经发送成功，请注意查收",
			)));
		} else {
			exit(json_encode(array(
				'error' => 1,
				'msg' => $r,
			)));
		}
	}

	public function content() {
		$topic_id = I('topic_id', "0", "intval");
		$this->data = BoxModel("sns_topic")->where(array('id' => $topic_id))->find();
		$this->web_title = $this->data['title'] . " - 考试突击";
		$this->display();
	}
}
